﻿using System.Linq;
using Domain.Entity;

namespace CommonDao
{
    public class SystemConfigDao
    {
      
        public static  void Insert(string key,string value)
        {

            using (var db = new DBDataContext())
            {
                SystemConfig config=new SystemConfig();
                config.Key = key;
                config.Value = value;
                db.SystemConfig.InsertOnSubmit(config);
                db.SubmitChanges();
            }
        }

        public static void Delete(string key)
        {
           
        }

        public static void Update(string key, string value)
        {
            Get(key);
            using (var db = new DBDataContext())
            {
                var obj = db.SystemConfig.FirstOrDefault(x => x.Key == key);
                if (obj != null) obj.Value = value;
                db.SubmitChanges();
            }

        }
        public static  string Get(string key)
        {
            using (var db = new DBDataContext())
            {
                var obj = db.SystemConfig.FirstOrDefault(x => x.Key == key);
                if (obj == null)
                {
                    Insert(key, string.Empty);
                    return string.Empty;
                }
                else
                {
                    return obj.Value.ToString();
                }
            }
            
            
        }
    }
}
